package com.zitangkou.user.controller;

import com.zitangkou.user.entity.EnterpriseDo;
import com.zitangkou.common.enums.RespEnum;
import com.zitangkou.common.global.GlobalResp;
import com.zitangkou.common.global.PageResp;
import com.zitangkou.user.req.enterprise.AddEnterpriseReq;
import com.zitangkou.user.req.enterprise.EditEnterpriseReq;
import com.zitangkou.user.req.enterprise.GetEnterpriseByPageReq;
import com.zitangkou.user.service.IEnterpriseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 企业模块
 *
 * @author Deng Ningning
 * @version 1.0
 * @date 2020/8/31 16:39
 */

@RestController
@RequestMapping("/enterprise")
public class EnterpriseController {

    private final IEnterpriseService enterpriseService;

    @Autowired
    public EnterpriseController(IEnterpriseService enterpriseService) {
        this.enterpriseService = enterpriseService;
    }

    /**
     * 分页查询企业
     *
     * @param req
     * @return com.zitangkou.common.global.GlobalResp<com.zitangkou.common.global.PageResp < com.zitangkou.user.model.entity.EnterpriseDO>>
     * @author Deng Ningning
     * @date 2020/9/1 17:24
     */
    @PostMapping("/list/page")
    public GlobalResp<PageResp<EnterpriseDo>> getEnterpriseByPage(
            @RequestBody @Validated GetEnterpriseByPageReq req) {
        return GlobalResp.success(enterpriseService.selectByPage(req));
    }

    /**
     * 添加企业
     *
     * @param req
     * @return com.zitangkou.common.global.GlobalResp
     * @author Deng Ningning
     * @date 2020/9/2 11:51
     */
    @PostMapping("/add")
    public GlobalResp<String> addEnterprise(
            @RequestBody @Validated AddEnterpriseReq req) {
        return enterpriseService.insertEnterprise(req)
                ? GlobalResp.success() : GlobalResp.fail(RespEnum.ADD_ENTERPRISE_FAIL);
    }

    /**
     * 修改企业
     *
     * @param req
     * @return com.zitangkou.common.global.GlobalResp
     * @author Deng Ningning
     * @date 2020/9/2 11:53
     */
    @PutMapping("/edit")
    public GlobalResp<String> editEnterprise(
            @RequestBody @Validated EditEnterpriseReq req) {
        return enterpriseService.updateEnterprise(req)
                ? GlobalResp.success() : GlobalResp.fail(RespEnum.EDIT_ENTERPRISE_FAIL);
    }

    /**
     * 删除企业
     *
     * @param enterpriseId
     * @return com.zitangkou.common.global.GlobalResp
     * @author Deng Ningning
     * @date 2020/9/2 11:55
     */
    @DeleteMapping("/remove/{enterpriseId}")
    public GlobalResp<String> removeEnterprise(
            @PathVariable String enterpriseId) {
        return enterpriseService.deleteEnterprise(enterpriseId)
                ? GlobalResp.success() : GlobalResp.fail(RespEnum.DELETE_ENTERPRISE_FAIL);
    }

    /**
     * 查询企业详情
     *
     * @param enterpriseId
     * @return com.zitangkou.common.global.GlobalResp<com.zitangkou.user.model.entity.EnterpriseDO>
     * @author Deng Ningning
     * @date 2020/9/2 11:56
     */
    @GetMapping("/get/{enterpriseId}")
    public GlobalResp<EnterpriseDo> getEnterprise(
            @PathVariable String enterpriseId) {
        return GlobalResp.success(enterpriseService.selectById(enterpriseId));
    }

}
